package com.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import com.entity.UserEntity;
import com.utils.ValidatorUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.annotation.IgnoreAuth;
import com.entity.WeibaojihuaEntity;
import com.entity.view.WeibaojihuaView;
import com.service.WeibaojihuaService;
import com.service.TokenService;
import com.utils.PageUtils;
import com.utils.R;
import com.utils.MPUtil;
import com.utils.CommonUtil;

/**
 * 维保计划Controller
 *后端接口
 */
@RestController
@RequestMapping("/weibaojihua")
public class WeibaojihuaController {
    @Autowired
    private WeibaojihuaService weibaojihuaService;
    
    
            /**
         * 后端列表
         */
                    @RequestMapping("/page")
        public R page(@RequestParam Map<String, Object> params, WeibaojihuaEntity weibaojihua, HttpServletRequest request) {
                            if (!request.getSession().getAttribute("role").toString().equals("管理员")) {
                    weibaojihua.setUserid((Long) request.getSession().getAttribute("userId"));
                }
                                    EntityWrapper<WeibaojihuaEntity> ew = new EntityWrapper<WeibaojihuaEntity>();
            PageUtils page = weibaojihuaService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, weibaojihua), params), params));
            request.setAttribute("data", page);
            return R.ok().put("data", page);
        }

        /**
         * 前端列表
         */
                                @RequestMapping("/list")
        public R list(@RequestParam Map<String, Object> params, WeibaojihuaEntity weibaojihua, HttpServletRequest request) {



                                    EntityWrapper<WeibaojihuaEntity> ew = new EntityWrapper<WeibaojihuaEntity>();
            PageUtils page = weibaojihuaService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, weibaojihua), params), params));
            request.setAttribute("data", page);
            return R.ok().put("data", page);
        }

        /**
             * 列表
             */
                    @RequestMapping("/lists")
        public R list( WeibaojihuaEntity weibaojihua) {
            EntityWrapper<WeibaojihuaEntity> ew = new EntityWrapper<WeibaojihuaEntity>();
            ew.allEq(MPUtil.allEQMapPre( weibaojihua, "weibaojihua"));
            return R.ok().put("data", weibaojihuaService.selectListView(ew));
        }

        /**
         * 查询
         */
                    @RequestMapping("/query")
        public R query(WeibaojihuaEntity weibaojihua) {
            EntityWrapper< WeibaojihuaEntity> ew = new EntityWrapper< WeibaojihuaEntity>();
            ew.allEq(MPUtil.allEQMapPre( weibaojihua, "Weibaojihua"));
                WeibaojihuaView weibaojihuaView = weibaojihuaService.selectView(ew);
            return R.ok("维保计划").put("data", weibaojihuaView);
        }

        /**
             * 后端详情
             */
                                @RequestMapping("/info/{id}")
        public R info(@PathVariable("id") Long id) {
                WeibaojihuaEntity weibaojihua = weibaojihuaService.selectById(id);
            return R.ok().put("data", weibaojihua);
        }

        /**
            * 前端详情
            */
                                @RequestMapping("/detail/{id}")
        public R detail(@PathVariable("id") Long id) {
                WeibaojihuaEntity weibaojihua = weibaojihuaService.selectById(id);
                        return R.ok().put("data", weibaojihua);
        }

        /**
        * 后端保存
        */
                    @RequestMapping("/save")
        public R save(@RequestBody WeibaojihuaEntity weibaojihua, HttpServletRequest request) {
                        weibaojihua.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
            //ValidatorUtils.validateEntity(address);
                            weibaojihua.setUserid((Long) request.getSession().getAttribute("userId"));
                                                            weibaojihuaService.insert(weibaojihua);
            return R.ok();
        }

        /**
            * 前端保存
            */
                    @RequestMapping("/add")
        public R add(@RequestBody WeibaojihuaEntity weibaojihua, HttpServletRequest request) {
                        
                weibaojihua.setUserid((Long) request.getSession().getAttribute("userId"));

                        weibaojihua.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
            //ValidatorUtils.validateEntity(yiyuanjianjie);

                weibaojihuaService.insert(weibaojihua);
            return R.ok();
        }

        /**
          * 修改
          */
                    @RequestMapping("/update")
        public R update(@RequestBody WeibaojihuaEntity weibaojihua, HttpServletRequest request) {
            //ValidatorUtils.validateEntity(zaixianbaoming);
                weibaojihuaService.updateById(weibaojihua);//全部更新
            return R.ok();
        }

        /**
             * 删除
             */
                    @RequestMapping("/delete")
        public R delete(@RequestBody Long[] ids) {
                weibaojihuaService.deleteBatchIds(Arrays.asList(ids));
            return R.ok();
        }
    

    
    
}
